package com.zsck.pos.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.ums.AppHelper;
import com.ums.anypay.service.IOnTransEndListener;
import com.chinaums.commoncheck.R;
import com.zsck.pos.base.BaseTitleActivity;
import com.zsck.pos.common.Constants;
import com.zsck.pos.net.MyObserver;
import com.zsck.pos.utils.LogUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ErrorActivity extends BaseTitleActivity {

    /*去支付*/
    @BindView(R.id.tv_confirm)
    TextView mTvSure;

    private String mBillIdList;



    @Override
    public int setLayout() {
        return R.layout.activity_error;
    }

    @Override
    public String setTitle() {
        return "收款金额";
    }

    @Override
    public Activity setActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initEvents();
        initData();
    }


    private void initEvents(){
        mTvSure.setOnClickListener(this);
    }
    private void initData(){
        Intent intent = getIntent();
        mBillIdList = intent.getStringExtra("idList");
        //getDetail();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case  R.id.tv_confirm:
                setResult(RESULT_OK);
                finish();
                break;
            case R.id.btn_records:

                break;
        }
    }





    private void startCallTrans() {
        String transAppName = "POS 通";
        String transAppId = "扫一扫";
        JSONObject transData = new JSONObject();
        try {
            transData.put("amt", 1);//金额
            transData.put("appId", Constants.APPID);//appId
            transData.put("isNeedPrintReceipt", false);//交易完是否自动打印签购单，注意true表示不自动打单，false表示自动打单。若选择打单则将按照银商标准签购单样式打印，若选择不自动打单则可根据支付结果自行调用打印接口打印，请参考SDK脚本打印范例程序。
        } catch (JSONException e) {
            LogUtil.e("ErrorActivity",e.getMessage());
        }
        //所有的支付应用都通过callTrans调起
        AppHelper.callTrans(this, transAppName, transAppId, transData, listener);
    }

    private void getDetail(){
        Map<String,Object> map = new HashMap<>();
        map.put("billIdList",mBillIdList);
        MyObserver<Object> observer = new MyObserver<Object>(this) {
            @Override
            public void onSuccess(Object result) {
                onGetinDetail(result);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                LogUtil.e("test",e.getMessage());
                Toast.makeText(ErrorActivity.this,errorMsg,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void reRequest() {
                getDetail();
            }
        };
//        RetrofitCilent.getApiService().getDetail(map).
//                compose(observableIO2Main(this)).subscribe(observer);
    }


    private void onGetinDetail(Object result){
        if(result == null){
            return;
        }
    }


    /**
     * 支付接口调用结果监听**/
    IOnTransEndListener listener = new IOnTransEndListener() {
        @Override
        public void onEnd(String reslutmsg) {//reslutmsg接口调用结果
            Message msg = new Message();
            msg.what = 0x77;
            msg.obj = reslutmsg;

            try {
                //提取返回结果相关字段
                JSONObject resultData = new JSONObject(reslutmsg);
                Object appNameStr= resultData.get(AppHelper.TRANS_APP_NAME);
                Object bizIdStr = resultData.get(AppHelper.TRANS_BIZ_ID);
                Object resultCodeStr = resultData.get(AppHelper.RESULT_CODE);
                Object resultMsgStr = resultData.get(AppHelper.RESULT_MSG);
                Object transDataStr = resultData.get(AppHelper.TRANS_DATA);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };


}
